package com.fenghongzhang.day002;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 单例  -> 懒 恶 -> 多线程
 * 双重
 */
public class SpUtils {

    private static final String LOGIN = "login";

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    private SpUtils(){
        sharedPreferences = App.context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    };

    private static SpUtils spUtils ;

    //Instance 实例
    public static SpUtils getInstance(){
        if(spUtils == null){
            synchronized (SpUtils.class){
                if(spUtils == null){
                    spUtils = new SpUtils();
                }
            }
        }
        return spUtils;
    }


    public void put(String key,Object o){
        if(o instanceof String){
            editor.putString(key, (String) o);
        }else if(o instanceof Integer){
            editor.putInt(key, (Integer) o);
        }
        editor.commit();
    }


    public SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }
}
